//
//  MyCourseViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/6/2.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "MyCourseViewController.h"
#import <FSCalendar/FSCalendar.h>
#import "CalendarRecordCell.h"
#import "NoticeListViewController.h"
#import "MyCourseListViewModel.h"
#import "CalendarRecordCell.h"
#import "UploadImageViewController.h"
#import "UserUseListModel.h"
#import "ImageBrowserViewController.h"
@interface MyCourseViewController ()<FSCalendarDelegate,FSCalendarDataSource,FSCalendarDelegateAppearance,UITableViewDelegate,UITableViewDataSource,CalendarRecordCellDelegate>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)UITableViewCell *calendarCell;
@property(nonatomic,strong)CalendarRecordCell *recordCell;
@property(nonatomic,strong)FSCalendar *calender;
@property(nonatomic,strong)NSCalendar *chineseCalendar;
@property(nonatomic,strong)UIImageView *whitchMonth;
//选中记录选项
@property(nonatomic,strong)NSString *analyse_type_str;
@property(nonatomic,strong)NSDate *selectDate;
@property(nonatomic,strong)NSDate *selectMonth;
@property(nonatomic,assign)BOOL haveData;
@property(nonatomic,strong)NSMutableArray *userUseData;
@property(nonatomic,strong)NSMutableArray *hansImageDate;
@property(nonatomic,strong)NSMutableArray *menstruationDate;
@property(nonatomic,strong)NSMutableArray *userUseList;
@property(nonatomic,strong)MyCourseListViewModel *courseViewModel;

@end

@implementation MyCourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"我的历程";
    [self configTableView];
    [self initialVar];
    [self setNavRightBtnForImage:[UIImage imageNamed:@"icon-system-fn-time"]];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.courseViewModel fetchMyCourseList:[NSString stringWithFormat:@"%ld",[NSDate date].year] month:[NSString stringWithFormat:@"%ld",[NSDate date].month] cmp:^(NSArray * _Nonnull userUseDates, NSArray * _Nonnull hansImageDate, NSArray * _Nonnull menstruationDate) {
        [self.userUseData removeAllObjects];
        [self.userUseData addObjectsFromArray:userUseDates];
        [self.hansImageDate removeAllObjects];
        [self.hansImageDate addObjectsFromArray:hansImageDate];
        [self.menstruationDate removeAllObjects];
        [self.menstruationDate addObjectsFromArray:menstruationDate];
        [self.calender reloadData];
    }];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    [self.courseViewModel fetchUserDateList:[dateFormatter stringFromDate:[NSDate date]] cmp:^(NSArray * _Nonnull userUseList) {
        [self.userUseList removeAllObjects];
        [self.userUseList addObjectsFromArray:userUseList];
        [self.mainTableView reloadData];
    }];
}
-(void)initialVar{
    self.userUseData = [[NSMutableArray alloc]init];
    self.chineseCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierRepublicOfChina];
    self.selectDate = [NSDate date];
    self.selectMonth = [NSDate date];
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
    UIView *footer = [UIView new];
    self.mainTableView.tableFooterView = footer;
    [self.mainTableView registerNib:[UINib nibWithNibName:@"CalendarRecordCell" bundle:nil] forCellReuseIdentifier:@"CalendarRecordCell"];
}
-(void)rightbuttonPressed:(id)sender{
    NoticeListViewController *noticeList = [[NoticeListViewController alloc]init];
    [self.rt_navigationController pushViewController:noticeList animated:YES complete:nil];
}
#pragma mark UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 315;
    }else{
        return 374;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return self.calendarCell;
    }else{
        CalendarRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CalendarRecordCell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor clearColor];
        [cell setCellValue:self.userUseList];
        cell.delegate = self;
        return cell;
    }
}
-(void)addFeelText:(UserUseListModel *)model{
    if (EC_ISNullStr(model.useFeel)) {
        UploadImageViewController *uploadImage = [[UploadImageViewController alloc]init];
        uploadImage.model = model;
        [self.rt_navigationController pushViewController:uploadImage animated:YES complete:nil];
    }
}
-(void)addFeel:(UserUseListModel *)model{
    if (model.hasImage) {
        ImageBrowserViewController *imageBrowser = [[ImageBrowserViewController alloc]init];
        imageBrowser.imgUrl = model.usePicture;
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:imageBrowser];
        [self presentViewController:nav animated:YES completion:nil];
    }else{
        UploadImageViewController *uploadImage = [[UploadImageViewController alloc]init];
        uploadImage.model = model;
        [self.rt_navigationController pushViewController:uploadImage animated:YES complete:nil];
    }
}
#pragma mark - cells
-(UITableViewCell*)calendarCell{
    if (!_calendarCell) {
        _calendarCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
        _calendarCell.selectionStyle = UITableViewCellSelectionStyleNone;
        _calendarCell.backgroundColor = [UIColor clearColor];
        UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, DEVICE_WIDTH-40, 315)];
        [bgView setImage:[[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"cornerBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"cornerBg"].size.height/2]];
        [_calendarCell.contentView addSubview:bgView];
        
        
        self.whitchMonth.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg%ld",[NSDate date].month]];
        [_calendarCell.contentView addSubview:self.whitchMonth];
        
        [_calendarCell.contentView addSubview:self.calender];
        
        UIImageView *dot1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, bgView.height-19, 4, 4)];
        dot1.backgroundColor = Color(@"FFA8A0");
        dot1.layer.cornerRadius = 2;
        [bgView addSubview:dot1];
        
        UILabel *dotLab1 = [[UILabel alloc]initWithFrame:CGRectMake(dot1.right+4, dot1.top+(dot1.height-12)/2, 24, 12)];
        dotLab1.textColor = Color(@"333333");
        dotLab1.text = @"美光";
        dotLab1.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
        [bgView addSubview:dotLab1];
        
        UIImageView *dot2 = [[UIImageView alloc]initWithFrame:CGRectMake(dotLab1.right+20, bgView.height-19, 4, 4)];
        dot2.backgroundColor = Color(@"115296");
        dot2.layer.cornerRadius = 2;
        [bgView addSubview:dot2];
        
        UILabel *dotLab2 = [[UILabel alloc]initWithFrame:CGRectMake(dot2.right+4, dot2.top+(dot2.height-12)/2, 24, 12)];
        dotLab2.textColor = Color(@"333333");
        dotLab2.text = @"图片";
        dotLab2.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
        [bgView addSubview:dotLab2];
        
        UIImageView *dot3 = [[UIImageView alloc]initWithFrame:CGRectMake(dotLab2.right+20, bgView.height-19, 4, 4)];
        dot3.backgroundColor = Color(@"16979D");
        dot3.layer.cornerRadius = 2;
        [bgView addSubview:dot3];
        
        UILabel *dotLab3 = [[UILabel alloc]initWithFrame:CGRectMake(dot3.right+4, dot3.top+(dot3.height-12)/2, 24, 12)];
        dotLab3.textColor = Color(@"333333");
        dotLab3.text = @"经期";
        dotLab3.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
        [bgView addSubview:dotLab3];
    }
    return _calendarCell;
}
-(FSCalendar*)calender{
    if (!_calender) {
        _calender = [[FSCalendar alloc]initWithFrame:CGRectMake(20, 0, DEVICE_WIDTH-40, 240)];
        _calender.backgroundColor = [UIColor clearColor];
        _calender.locale = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
        _calender.headerHeight = 40;
        
        _calender.delegate = self;
        _calender.dataSource = self;
        
        _calender.appearance.weekdayTextColor = Color(@"666666");
        _calender.appearance.headerTitleColor = Color(@"666666");
        _calender.appearance.borderRadius = 10;
        _calender.appearance.titleFont = [UIFont systemFontOfSize:16];
        
        _calender.appearance.selectionColor = Color(@"FFA8A0");
        
        _calender.appearance.headerMinimumDissolvedAlpha = 0;
        
        _calender.appearance.todayColor = Color(@"FE796A");
        _calender.appearance.todaySelectionColor = Color(@"F45142");
        
        _calender.appearance.titleSelectionColor = [UIColor whiteColor];
        _calender.appearance.titleDefaultColor = Color(@"595959");
        _calender.appearance.titlePlaceholderColor = [UIColor colorWithRed:0.667 green:0.875 blue:1.000 alpha:1.00];
        
        [_calender selectDate:[NSDate date]];
        _calender.appearance.headerDateFormat = @"MM月";
        _calender.scope = FSCalendarScopeMonth;
        _calender.calendarHeaderView.userInteractionEnabled = YES;
        
        UIButton *arrowLeft = [[UIButton alloc]initWithFrame:CGRectMake(10, _calender.headerHeight/2-30/2, 30, 30)];
        [arrowLeft addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [arrowLeft setImage:[UIImage imageNamed:@"arrowLeft"] forState:UIControlStateNormal];
        [_calender addSubview:arrowLeft];
        
        UIButton *arrowRight = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, _calender.headerHeight/2-30/2, 30, 30)];
        [arrowRight addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [arrowRight setImage:[UIImage imageNamed:@"arrowRight"] forState:UIControlStateNormal];
        [_calender addSubview:arrowRight];
    }
    return _calender;
}
-(void)leftButtonPressed:(id)sender{
    NSDate *nextMonth = [self.chineseCalendar dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:self.calender.currentPage options:0];
    [self.calender setCurrentPage:nextMonth animated:YES];
}
-(void)rightButtonPressed:(id)sender{
    NSDate *nextMonth = [self.chineseCalendar dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:self.calender.currentPage options:0];
    [self.calender setCurrentPage:nextMonth animated:YES];
}
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar{
    self.whitchMonth.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg%ld",calendar.currentPage.month]];
    [self.courseViewModel fetchMyCourseList:[NSString stringWithFormat:@"%ld",calendar.currentPage.year] month:[NSString stringWithFormat:@"%ld",calendar.currentPage.month] cmp:^(NSArray * _Nonnull userUseDates, NSArray * _Nonnull hansImageDate, NSArray * _Nonnull menstruationDate) {
        [self.userUseData removeAllObjects];
        [self.userUseData addObjectsFromArray:userUseDates];
        [self.hansImageDate removeAllObjects];
        [self.hansImageDate addObjectsFromArray:hansImageDate];
        [self.menstruationDate removeAllObjects];
        [self.menstruationDate addObjectsFromArray:menstruationDate];
        [calendar reloadData];
    }];
}
-(void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    [self.courseViewModel fetchUserDateList:[dateFormatter stringFromDate:date] cmp:^(NSArray * _Nonnull userUseList) {
        [self.userUseList removeAllObjects];
        [self.userUseList addObjectsFromArray:userUseList];
        [self.mainTableView reloadData];
    }];
}
// 对有事件的显示一个点,默认是显示三个点
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    return [self numberOfDot:[dateFormatter stringFromDate:date]];
}
- (nullable NSArray<UIColor *> *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventDefaultColorsForDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    return [self colorsOfDot:[dateFormatter stringFromDate:date]];
}
- (nullable NSArray<UIColor *> *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventSelectionColorsForDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    return [self colorsOfDot:[dateFormatter stringFromDate:date]];
}
-(NSInteger)numberOfDot:(NSString*)dateString{
    NSInteger dotNumber = 0;
    if ([self.userUseData containsObject:dateString]) {
        dotNumber += 1;
    }
    if ([self.hansImageDate containsObject:dateString]) {
        dotNumber += 1;
    }
    if ([self.menstruationDate containsObject:dateString]) {
        dotNumber += 1;
    }
    return dotNumber;
}
-(NSArray*)colorsOfDot:(NSString*)dateString{
    NSMutableArray* colorArray = [[NSMutableArray alloc]init];
    if ([self.userUseData containsObject:dateString]) {
        [colorArray addObject:[UIColor colorWithHexString:@"FFA8A0"]];
    }
    if ([self.hansImageDate containsObject:dateString]) {
        [colorArray addObject:[UIColor colorWithHexString:@"115296"]];
    }
    if ([self.menstruationDate containsObject:dateString]) {
        [colorArray addObject:[UIColor colorWithHexString:@"16979D"]];
    }
    return colorArray;
}
#pragma mark - instance
-(MyCourseListViewModel*)courseViewModel{
    if (!_courseViewModel) {
        _courseViewModel = [[MyCourseListViewModel alloc]init];
    }
    return _courseViewModel;
}
-(UIImageView*)whitchMonth{
    if (!_whitchMonth) {
        _whitchMonth = [[UIImageView alloc]initWithFrame:CGRectMake((DEVICE_WIDTH-40-145)/2, (240-145)/2, 145, 145)];
    }
    return _whitchMonth;
}
-(NSMutableArray*)userUseList{
    if (!_userUseList) {
        _userUseList = [[NSMutableArray alloc]init];
    }
    return _userUseList;
}
-(NSMutableArray*)hansImageDate{
    if (!_hansImageDate) {
        _hansImageDate = [[NSMutableArray alloc]init];
    }
    return _hansImageDate;
}
-(NSMutableArray*)menstruationDate{
    if (!_menstruationDate) {
        _menstruationDate = [[NSMutableArray alloc]init];
    }
    return _menstruationDate;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
